﻿using B.S.DataBase.MyProject.Domain.productionworkorder;
using B.S.DataBase.MyProject.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using T.E.ERP.Write.API.Applicantions.Command.ApprovalCommand;
using T.E.ERP.Write.API.Applicantions.Command.BOMCommand;
using T.E.ERP.Write.API.Applicantions.Command.SCWorkOrderCommand;
using T.E.ERP.Write.API.Applicantions.Command.ZJWorkOrderCommand;

namespace T.E.ERP.Write.API.Controllers
{
    /// <summary>
    /// 工单控制器-写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WrokOrderController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        public WrokOrderController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        #region BOM管理
        /// <summary>
        /// BOM信息添加
        /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddBom(AddBomInfoCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改BOM工单
        /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdateBom(UpdateBomCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加物料
        /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddParts(AddBomPartsCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 工艺流程添加
        /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddProcess(ProcessInfoCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 工艺流程修改
        /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateProocessInfo(UpdateProcessCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region 生产工单
        /// <summary>
        /// 生产工单添加
        /// 物联网-智能MES生产管理系统-09-生产工单管理V1.1-20250206
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> SCWorkOrderAdd(SCWorkOrderAddCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 生产工单质检的操作
        /// 物联网-智能MES生产管理系统-09-生产工单管理V1.1-20250206
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> SCWorkOrderZJ(SCWorkOrderZJCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 上传图片/文件
        /// 物联网-智能MES生产管理系统-09-生产工单管理V1.1-20250206
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadPic(IFormFile file)
        {
            try
            {
                var res = "wwwroot/img/" + file.FileName;
                using (FileStream fs = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("http://localhost:5090/img/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }




        #endregion

        #region 质检管理
        /// <summary>
        /// 质检工单基本信息的添加
        /// 物联网-智能MES生产管理系统-10-质检管理V1.1-20250206
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<QualityInspectionWorkOrder>> ZJWorkOrderAdd(ZJWorkOrderAddCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion



        #region 审批管理


        /// <summary>
        /// 审批管理新增节点
        /// </summary>
        /// <param name="approval"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> PrrApproval(ApprovalTrailCommand approval)
        {
            return await mediator.Send(approval);
        }


        /// <summary>
        /// 审批管理更改
        /// </summary>
        /// <param name="approval"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> PutApproval(UpdaterovalTrailCommand approval)
        {
            return await mediator.Send(approval);
        }
        #endregion

    }
}
